package org.mtmi.ui.scenes;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;

/**
 * Singleton class to handle properly fonts for controls and scene.
 * {@link #dispose()} method should be called when no more fonts are be used.
 * @author jgout
 *
 */
public class FontManager {

	private static FontManager instance;
	private Font normal, list, title, toolbar;

	private FontManager() {
		normal = createFont(30,SWT.NORMAL);
		list = createFont(20,SWT.NORMAL);
		title = createFont(30,SWT.BOLD);
		toolbar = createFont(16,SWT.NORMAL);
	}

	/**
	 * Returns a new font with the given size.
	 * This returned font should be disposed by the caller.
	 * @return
	 */
	private Font createFont(int size, int style) {
		Display display = Display.getDefault();
		Font font = display.getSystemFont();
		FontData[] fontData = font.getFontData();
		for (int i = 0; i < fontData.length; i++) {
			fontData[i].setHeight(size);
			fontData[i].setStyle( style );
		}
		font = new Font(display, fontData);
		return font;
	}
	
	public static FontManager getInstance() {
		if(instance == null) {
			instance = new FontManager();
		}
		return instance;
	}
	
	public Font getNormalFont() {
		return normal;
	}
	
	public Font getListFont() {
		return list;
	}
	
	public Font getTitleFont() {
		return title;
	}
	
	public Font getToolBarFont() {
		return toolbar;
	}
	
	public void dispose() {
		if(normal != null && !normal.isDisposed()) {
			normal.dispose();
		}
		if(list != null && !list.isDisposed()) {
			list.dispose();
		}
		if(title != null && !title.isDisposed()) {
			title.dispose();
		}
		if(toolbar != null && !toolbar.isDisposed()) {
			toolbar.dispose();
		}
	}
}
